#include "../include/cyrobot_monitor/settings.h"
#include "ui_settings.h"

Settings::Settings(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Settings)
{
    ui->setupUi(this);

    QSettings main_setting("topic_setting","cyrobot_monitor");
    ui->lineEdit_odm->setText(main_setting.value("topic_odom","raw_odom").toString());
    ui->lineEdit_power->setText(main_setting.value("topic_power","power").toString());
    ui->lineEdit_power_min->setText(main_setting.value("power_min","21").toString());
    ui->lineEdit_power_max->setText(main_setting.value("power_max","24").toString());
//    ui->lineEdit_odom->setText(main_setting.value("odom_topic","amcl_pose").toString());

    QSettings power_setting("gopower_setting","cyrobot_monitor");
    ui->go_power_lineEdit->setText(power_setting.value("go_power","20").toString());
    ui->go_power_checkBox->setChecked(power_setting.value("power_check","false").toBool());


    connect(ui->pushButton,SIGNAL(clicked()),this,SLOT(slot_ok_btn_click()));
    connect(ui->pushButton_2,SIGNAL(clicked()),this,SLOT(slot_cancel_btn_click()));
}
void Settings::slot_ok_btn_click()
{
    QSettings main_setting("topic_setting","cyrobot_monitor");
    main_setting.setValue("topic_odom",ui->lineEdit_odm->text());
    main_setting.setValue("topic_power",ui->lineEdit_power->text());
    main_setting.setValue("power_min",ui->lineEdit_power_min->text());
    main_setting.setValue("power_max",ui->lineEdit_power_max->text());
//    main_setting.setValue("odom_topic",ui->lineEdit_odom->text());

    QSettings power_setting("gopower_setting","cyrobot_monitor");
    power_setting.setValue("go_power",ui->go_power_lineEdit->text());
    power_setting.setValue("power_check",ui->go_power_checkBox->checkState());

    QMessageBox::critical(NULL, "保存成功！", "保存成功，部分功能需重启后生效！", QMessageBox::Yes | QMessageBox::No, QMessageBox::Yes);
    this->close();
}
void Settings::slot_cancel_btn_click()
{
    this->close();
}
Settings::~Settings()
{
    delete ui;
}
